namespace Brjnk.Maps
{
    using System;

    /// <summary>
    /// </summary>
    public struct TicksTime
    {
        public const Int64 TicksPerSecond = 1000;
        public const Int64 TicksPerHour = 1000L * 60 * 60;
        public const Int64 TicksPerMinute = 1000 * 60;
        public const Int64 TicksPerMillisecond = 1;
        public const Int64 TicksPerDay = 1000L * 60 * 60 * 24;

        private Int64 ticks;

        public TicksTime(Int64 milliseconds)
        {
            this.ticks = milliseconds;
        }

        public TicksTime(double milliseconds)
        {
            this.ticks = (Int64)milliseconds;
        }

        public TicksTime AddMilliseconds(long ms)
        {
            return new TicksTime(ticks + ms * TicksPerMillisecond);
        }

        public TicksTime AddMilliseconds(double ms)
        {
            return new TicksTime(ticks + ms * TicksPerMillisecond);
        }

        public TicksTime AddSeconds(int s)
        {
            return new TicksTime(ticks + s * TicksPerSecond);
        }

        public Int64 Ticks
        {
            get { return ticks; }
        }

        public override string ToString()
        {
            Int64 ticks = this.ticks;
            int day = (int)(ticks / TicksPerDay);
            ticks -= day * TicksPerDay;
            var time = TimeSpan.FromMilliseconds(ticks);
            //int hour = (int)(ticks / TicksPerHour);
            //ticks-=hour * TicksPerHour ;
            //int minute = (int)(ticks / TicksPerMinute);
            //ticks-=minute *TicksPerMinute;
            //double second = ticks /(double) TicksPerSecond;
            return string.Format("{0} {1:g}", day, time);
        }

        public static TicksTime Add(double ms)
        {
            return DefaultTimingProvider.GetDefaultTimingProvider().GetTime().AddMilliseconds(ms);
        }

        public static bool operator <=(TicksTime t1, TicksTime t2)
        {
            return t1.Ticks <= t2.Ticks;
        }

        public static bool operator >=(TicksTime t1, TicksTime t2)
        {
            return t1.Ticks >= t2.Ticks;
        }

        public static bool operator ==(TicksTime t1, TicksTime t2)
        {
            return t1.Ticks == t2.Ticks;
        }

        public static bool operator !=(TicksTime t1, TicksTime t2)
        {
            return t1.Ticks != t2.Ticks;
        }

        public static TimeSpan operator -(TicksTime t1, TicksTime t2)
        {
            return TimeSpan.FromMilliseconds(t2.Ticks - t1.Ticks);
        }

        public override bool Equals(object obj)
        {
            return this == (TicksTime)obj;
        }

        public override int GetHashCode()
        {
            return ticks.GetHashCode();
        }
    }
}